package com.secneo.appshare.threads;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.secneo.appshare.api.RemoteApi.AppRecom;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Util;

public class TopListThread extends Thread{
	private static final String TAG = "MyTag";
	private Context ctx;
	private Handler handler;
	private String uid;
	private int numOfApp;
	
	private List<AppRecom> mDownList;
	private List<AppRecom> mHotList;
	private List<AppRecom> mTalkList;
	private List<AppRecom> mPromoteList;
	private List<AppRecom> mGoodList;
	private boolean isStop;
	
	public synchronized void stopRun()
	{
		isStop = true;
		this.interrupt();
	}
	
	public TopListThread(Context c, Handler handler, String uid, int num) 
	{
		this.handler = handler;
		this.uid = uid;
		this.numOfApp = num;
		this.ctx = c;
		mDownList = null;
		mHotList = null;
		mTalkList = null;
		mPromoteList = null;
		mGoodList = null;
	}
	
	public List<AppRecom> getDownList() 
	{
		return mDownList;
	}
	
	public List<AppRecom> getHotList() 
	{
		return mHotList;
	}
	
	public List<AppRecom> getTalkList() 
	{
		return mTalkList;
	}
	
	public List<AppRecom> getPromoteList() 
	{
		return mPromoteList;
	}
	
	public List<AppRecom> getGoodList() 
	{
		return mGoodList;
	}
	
	public void run() 
	{
		this.isStop = false;
		try 
		{
			if(mDownList == null)
				mDownList = new ArrayList<AppRecom>();
			else
				mDownList.clear();
			if(mHotList == null)
				mHotList = new ArrayList<AppRecom>();
			else
				mHotList.clear();
			if(mTalkList == null)
				mTalkList = new ArrayList<AppRecom>();
			else
				mTalkList.clear();
			if(mPromoteList == null)
				mPromoteList = new ArrayList<AppRecom>();
			else
				mPromoteList.clear();
			if(mGoodList == null)
				mGoodList = new ArrayList<AppRecom>();
			else
				mGoodList.clear();
			
			RemoteApiImpl remoteApi = new RemoteApiImpl();
			remoteApi.getTopList(ctx, uid, numOfApp, mDownList, mHotList, mTalkList, mPromoteList, mGoodList);
			if (mDownList.size() == 0 && mHotList.size() == 0 && mTalkList.size() == 0 && mPromoteList.size() == 0 && mGoodList.size() == 0) 
			{
				Message msg = new Message();
				msg.what = Constants.MSG_GET_RECOMS_NETWORK_ERROR;
				handler.sendMessage(msg);
				return;
			}
			Log.d(TAG, "mDownList size is " + mDownList.size());
			Log.d(TAG, "mHotList size is " + mHotList.size());
			Log.d(TAG, "mTalkList size is " + mTalkList.size());
			Log.d(TAG, "mPromoteList size is " + mPromoteList.size());
			Log.d(TAG, "mGoodList size is " + mGoodList.size());
			if (isStop) 
				return;
			Message msg = new Message();
			msg.what = Constants.MSG_GET_TOPLIST_FINISH;
			handler.sendMessage(msg);
			
			// 开始下载图片
			int maxSize = Math.max(Math.max(Math.max(mTalkList.size(), mPromoteList.size()), Math.max(mDownList.size(), mHotList.size())), mGoodList.size());
			for(int i=0; i<maxSize; i++)
			{
				if (isStop) 
					return;
				
				if(i<mDownList.size())
				{
					try
					{
						AppRecom app =  mDownList.get(i);		
						Drawable dw = Util.getDrawableFromCache(ctx, app.app_icon);
						if (dw != null) 
						{
							app.app_icon_drawable = dw;
							Message iconmsg = new Message();
							iconmsg.what = Constants.MSG_GET_TOPLISTICON_FINISH;
							handler.sendMessage(iconmsg);
						}
					
					}catch(Exception ex) {
						Log.d(TAG, "TopListThread::Run() read icon error = "+ex.getMessage());
					}
				}
				
				if(i<mHotList.size() && !isStop)
				{
					try
					{
						AppRecom app =  mHotList.get(i);			
						Drawable dw = Util.getDrawableFromCache(ctx, app.app_icon);
						if (dw != null) 
						{
							app.app_icon_drawable = dw;
							Message iconmsg = new Message();
							iconmsg.what = Constants.MSG_GET_TOPLISTICON_FINISH;
							handler.sendMessage(iconmsg);
						}
					
					}catch(Exception ex) {
						Log.d(TAG, "TopListThread::Run() read icon error = "+ex.getMessage());
					}
				}
				
				if(i<mTalkList.size() && !isStop)
				{
					AppRecom app =  mTalkList.get(i);			
					try
					{
						String user_url = app.app_icon;
						if(user_url.contains("buildin"))
							app.app_icon_drawable = Util.getDrawableFromAsset(ctx, user_url);
						else
							app.app_icon_drawable = Util.getDrawableFromCache(ctx, user_url);
					
					}catch(Exception ex) {
						Log.d(TAG, "TopListThread::Run() read icon error = "+ex.getMessage());
					}
					
					if (app.app_icon_drawable != null) 
					{
						Message iconmsg = new Message();
						iconmsg.what = Constants.MSG_GET_TOPLISTICON_FINISH;
						handler.sendMessage(iconmsg);
					}
				}

				if(i<mPromoteList.size() && !isStop)
				{
					try
					{
						AppRecom app =  mPromoteList.get(i);			
						Drawable dw = Util.getDrawableFromCache(ctx, app.app_icon);
						if (dw != null) 
						{
							app.app_icon_drawable = dw;
							Message iconmsg = new Message();
							iconmsg.what = Constants.MSG_GET_TOPLISTICON_FINISH;
							handler.sendMessage(iconmsg);
						}
					
					}catch(Exception ex) {
						Log.d(TAG, "TopListThread::Run() read icon error = "+ex.getMessage());
					}
				}
				
				if(i<mGoodList.size() && !isStop)
				{
					AppRecom app =  mGoodList.get(i);			
					try
					{
						String user_url = app.app_icon;
						if(user_url.contains("buildin"))
							app.app_icon_drawable = Util.getDrawableFromAsset(ctx, user_url);
						else
							app.app_icon_drawable = Util.getDrawableFromCache(ctx, user_url);
					
					}catch(Exception ex) {
						Log.d(TAG, "TopListThread::Run() read icon error = "+ex.getMessage());
					}
					
					if (app.app_icon_drawable != null) 
					{
						Message iconmsg = new Message();
						iconmsg.what = Constants.MSG_GET_TOPLISTICON_FINISH;
						handler.sendMessage(iconmsg);
					}
				}
			}
			
		} catch(Exception ex) {
			Log.d(TAG, "TopListThread error:"+ex.getMessage());
		}
	}
}
